" ~/.vimrc
" => 全局配置

" 关闭兼容模式
set nocompatible

" 设置历史记录步数
set history=100

" 开启相关插件
filetype on
filetype plugin on
filetype indent on

" 当文件在外部被修改时，自动更新该文件
set autoread

" 激活鼠标的使用
set mouse=a

" 开启语法高亮
syntax enable

" 设置配色
colorscheme desert

" 高亮显示当前行
set cursorline
hi CursorLine guibg=#00ff00
hi CursorColumn guibg=#00ff00

" 激活折叠功能
set foldenable
" 按手工方式折叠
set foldmethod=manual
" 折叠区域宽度
set foldcolumn=0
" 折叠层数为3
set foldlevel=3
" 自动关闭折叠
set foldclose=all

" 用空格键开关折叠
nnoremap <space> za

" 使用空格替换Tab
set expandtab
" Tab和缩进为4个空格
set tabstop=4
set shiftwidth=4
set softtabstop=4
set smarttab

" 自动缩进
set autoindent
" 智能缩进
set smartindent
set si

" 自动换行
set wrap

" Vim 界面
set wildmenu
" 显示标尺
set ruler
" 命令行高度
set cmdheight=1
" 显示行号
set number
" 宏运行时不重绘
set lazyredraw

" 设置退格
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
set magic

" 关闭错误铃声
set noerrorbells
set novisualbell

" 显示匹配括号
set showmatch
set mat=2

" 搜索高亮
set hlsearch
set ignorecase

" 编码设置
set encoding=utf-8
set fileencodings=utf-8
set termencoding=utf-8

" 状态栏
set laststatus=2

" 粘贴模式切换
set pastetoggle=<F9>

" 设置背景色
set background=dark

" 高亮搜索
highlight Search ctermbg=black ctermfg=white guifg=white guibg=black

" 新建文件自动加头部
autocmd BufNewFile *.py,*.cc,*.sh,*.java call SetTitle()
function! SetTitle()
    if expand("%:e") == 'sh'
        call setline(1, "#!/bin/bash")
        call setline(2, "######################################")
        call setline(3, "#-| Author:BiaoZyx")
        call setline(4, "#-| Blog: http://cnblogs.com/BiaoZyx")
        call setline(5, "#-| Time:".strftime("%F %T"))
        call setline(6, "#-| Name:".expand("%"))
        call setline(7, "#-| Version：V1.0")
        call setline(8, "#-| Description:This is a test code.")
        call setline(9, "######################################")
        call setline(10,"")
    endif
    if expand("%:e") == 'py'
        call setline(1, "#!/bin/bash")
        call setline(2, "######################################")
        call setline(3, "#-| Author:BiaoZyx")
        call setline(4, "#-| Blog: http://cnblogs.com/BiaoZyx")
        call setline(5, "#-| Time:".strftime("%F %T"))
        call setline(6, "#-| Name:".expand("%"))
        call setline(7, "#-| Version：V1.0")
        call setline(8, "#-| Description:This is a test code.")
        call setline(9, "######################################")
        call setline(10,"")
    endif
endfunction

" Tab 补全
function! CleverTab()
    if strpart(getline('.'), 0, col('.')-1) =~ '^\s*$'
        return "\<Tab>"
    else
        return "\<C-N>"
    endif
endfunction
inoremap <Tab> <C-R>=CleverTab()<CR>
